// DlgEditStaff.cpp : implementation file
//

#include "stdafx.h"
#include "Celsus.h"
#include "DlgEditStaff.h"
#include "DlgChangePwd.h"
#include "DlgPickUser.h"
#include "DlgConfirm.h"

// CDlgEditStaff dialog

IMPLEMENT_DYNAMIC(CDlgEditStaff, CDialog)

CDlgEditStaff::CDlgEditStaff(CWnd* pParent /*=NULL*/)
	: CDialog(CDlgEditStaff::IDD, pParent)
	, m_szID(_T(""))
	, m_szDateLogin(_T(""))
	, m_szUserName(_T(""))
	, m_szUserEmail(_T(""))
	, m_szUserPhone(_T(""))
	, m_szUserLevel(_T("0"))
	, m_szLibraryID(_T("0"))
	, m_bShowTriage(FALSE)
	, m_bShowHighlights(TRUE)
	, m_bHighlightDuplicate(TRUE)
	, m_bSortAscending(TRUE)
	, m_bBoldEmailOnPrint(TRUE)
	, m_bShowLibraryView(TRUE)
	, m_bShowMyView(TRUE)
	, m_bShowInProcessView(FALSE)
	, m_bShowCurrentView(FALSE)
	, m_bShowCompletedView(FALSE)
	, m_bShowHoldView(FALSE)
	, m_bEnableThumbnail(TRUE)
	, m_szUserPassword(_T(""))
	, m_bUsePrintTemplate(FALSE)
	, m_nPrintPerPage(0)
	, m_bEnableDefaultCollection(TRUE)
	, m_bEnableDefaultILLLibrary(FALSE)
	, m_bPromptMaterial(false)
{
	m_bAddStaff = FALSE;
	m_bChangePwd = FALSE;
	m_bEditUser = FALSE;
	m_nStaffID = -1;
	m_nCollectionID = 0;
	m_nILLLibraryID = 0;
}

CDlgEditStaff::~CDlgEditStaff()
{
}

void CDlgEditStaff::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Text(pDX, IDC_EDIT_ID, m_szID);
	DDX_Text(pDX, IDC_EDIT_DATE_LOGIN, m_szDateLogin);
	DDX_Text(pDX, IDC_EDIT_USER_NAME, m_szUserName);
	DDX_Text(pDX, IDC_EDIT_USER_EMAIL, m_szUserEmail);
	DDX_Text(pDX, IDC_EDIT_PHONE, m_szUserPhone);
	DDX_Text(pDX, IDC_EDIT_LEVEL, m_szUserLevel);
	DDX_Text(pDX, IDC_EDIT_LIBRARY, m_szLibraryID);
	DDX_Check(pDX, IDC_CHECK_SHOW_TRIAGE, m_bShowTriage);
	DDX_Check(pDX, IDC_CHECK_SHOW_HIGHLIGHTS, m_bShowHighlights);
	DDX_Check(pDX, IDC_CHECK_SHOW_HIGHLIGHT_DUPLICATES, m_bHighlightDuplicate);
	DDX_Check(pDX, IDC_CHECK_SORT_ASCENDING, m_bSortAscending);
	DDX_Check(pDX, IDC_CHECK_PROMPT_MATERIAL, m_bPromptMaterial);
	DDX_Check(pDX, IDC_CHECK_BOLD_EMAIL_PRINT, m_bBoldEmailOnPrint);
	DDX_Check(pDX, IDC_CHECK_SHOW_LIBRARY_VIEW, m_bShowLibraryView);
	DDX_Check(pDX, IDC_CHECK_SHOW_MY_VIEW, m_bShowMyView);
	DDX_Check(pDX, IDC_CHECK_SHOW_IN_PROCESS_VIEW, m_bShowInProcessView);
	DDX_Check(pDX, IDC_CHECK_SHOW_CURRENT_VIEW, m_bShowCurrentView);
	DDX_Check(pDX, IDC_CHECK_SHOW_COMPLETED_VIEW, m_bShowCompletedView);
	DDX_Check(pDX, IDC_CHECK_SHOW_HOLD_VIEW, m_bShowHoldView);
	DDX_Check(pDX, IDC_CHECK_ENABLE_THUMBNAIL, m_bEnableThumbnail);
	DDX_Text(pDX, IDC_EDIT_PASSWORD, m_szUserPassword);
	DDX_Check(pDX, IDC_CHECK_USE_PRINT_TEMPLATE, m_bUsePrintTemplate);
	DDX_Text(pDX, IDC_EDIT_PRINT_PER_PAGE, m_nPrintPerPage);
	DDX_Control(pDX, IDC_COMBO_COLLECTION, m_ctrlCollection);
	DDX_Check(pDX, IDC_CHECK_ENABLE_DEFAULT_COLLECTION, m_bEnableDefaultCollection);
	DDX_Check(pDX, IDC_CHECK_ENABLE_DEFAULT_ILL_LIBRARY, m_bEnableDefaultILLLibrary);
	DDX_Control(pDX, IDC_COMBO_ILL_LIBRARY, m_ctrlILLLibrary);
}


BEGIN_MESSAGE_MAP(CDlgEditStaff, CDialog)
	ON_BN_CLICKED(IDC_BUTTON_PASSWORD, &CDlgEditStaff::OnBnClickedButtonPassword)
	ON_BN_CLICKED(IDC_BUTTON_DELETE, &CDlgEditStaff::OnBnClickedButtonDelete)
	ON_BN_CLICKED(IDC_CHECK_ENABLE_DEFAULT_COLLECTION, &CDlgEditStaff::OnBnClickedCheckEnableDefaultCollection)
	ON_BN_CLICKED(IDC_CHECK_ENABLE_DEFAULT_ILL_LIBRARY, &CDlgEditStaff::OnBnClickedCheckEnableDefaultIllLibrary)
END_MESSAGE_MAP()


// CDlgEditStaff message handlers

BOOL CDlgEditStaff::OnInitDialog()
{
	CDialog::OnInitDialog();

	// check if adding
	if( m_bAddStaff )
	{
		// show password controls
		GetDlgItem( IDC_STATIC_PASSWORD )->ShowWindow( SW_SHOW );
		GetDlgItem( IDC_EDIT_PASSWORD )->ShowWindow( SW_SHOW );
		GetDlgItem( IDC_BUTTON_PASSWORD )->ShowWindow( SW_HIDE );
		GetDlgItem( IDC_BUTTON_DELETE )->ShowWindow( SW_HIDE );
		return TRUE;
	}
	else
	{
		GetDlgItem( IDC_STATIC_PASSWORD )->ShowWindow( SW_HIDE );
		GetDlgItem( IDC_EDIT_PASSWORD )->ShowWindow( SW_HIDE );
		GetDlgItem( IDC_BUTTON_PASSWORD )->ShowWindow( SW_SHOW );
		GetDlgItem( IDC_BUTTON_DELETE )->ShowWindow( SW_SHOW );
	}

	// check if editing
	if( m_bEditUser )
	{
		GetDlgItem( IDC_STATIC_PASSWORD )->ShowWindow( SW_HIDE );
		GetDlgItem( IDC_EDIT_PASSWORD )->ShowWindow( SW_HIDE );
		GetDlgItem( IDC_BUTTON_PASSWORD )->ShowWindow( SW_SHOW );
		GetDlgItem( IDC_EDIT_USER_NAME )->EnableWindow( FALSE );
		GetDlgItem( IDC_EDIT_LEVEL )->EnableWindow( FALSE );
		GetDlgItem( IDC_BUTTON_DELETE )->ShowWindow( SW_HIDE );
	}

	// get staff
	STAFF_SET Staff;

	// pick list
	if( m_nStaffID < 0 )
	{
		CString szName;
		CDlgPickUser dlgPickUser;
		dlgPickUser.m_nType = dlgPickUser.PICK_TYPE_STAFF;
		if( dlgPickUser.DoModal() == IDOK )
			szName = dlgPickUser.m_szName;
		else
		{
			EndDialog( IDCANCEL );
			return TRUE;
		}
		// get staff
		appConfig.m_SQL.GetStaffSet( &Staff, szName );
	}
	else
	{
		appConfig.m_SQL.GetStaffSet( &Staff, m_nStaffID );
	}

	// set user id
	m_nStaffID = Staff.m_id;
	// set current
	m_szID.Format( _T("%.8ld"), Staff.m_id );
	m_szDateLogin = Staff.m_date_login.Format( appConfig.m_General.szDateFormat );
	m_szUserName = Staff.m_name;
	m_szUserEmail = Staff.m_email;
	m_szUserPassword = Staff.m_pwd;
	m_szUserPhone = Staff.m_phone;
	m_szUserLevel.Format( _T("%d"), Staff.m_level );
	m_szLibraryID.Format( _T("%d"), Staff.m_library_id );
	m_bShowTriage = (BOOL)Staff.m_show_triage;
	m_bShowLibraryView = (BOOL)Staff.m_show_library_view;
	m_bShowMyView = (BOOL)Staff.m_show_my_view;
	m_bShowInProcessView = (BOOL)Staff.m_show_process_view;
	m_bShowCurrentView = (BOOL)Staff.m_show_current_view;
	m_bShowCompletedView = (BOOL)Staff.m_show_completed_view;
	m_bShowHoldView = (BOOL)Staff.m_show_hold_view;
	m_bShowHighlights = (BOOL)Staff.m_show_highlights;
	m_bHighlightDuplicate = (BOOL)Staff.m_show_duplicates;
	m_bSortAscending = (BOOL)Staff.m_sort_ascending;
	m_bBoldEmailOnPrint = (BOOL)Staff.m_bold_email_print;
	m_bEnableThumbnail = (BOOL)Staff.m_enable_thumbnail;
	m_bUsePrintTemplate = (BOOL)Staff.m_use_print_template;
	m_nPrintPerPage = Staff.m_print_per_page;
	m_nCollectionID = Staff.m_default_collection;
	m_nILLLibraryID = Staff.m_default_ill_library;

	INT nCurSel = 0;
	INT nSel = 0;
	CString szTemp;

	// get library descriptions
	appConfig.m_SQL.GetDescriptions( CMySQLEx::collection, &m_szaCollection, CMySQLEx::list );
	// set m_ctrlCollection
	m_ctrlCollection.Init();
	m_ctrlCollection.SetMode();
	for( INT nIndex = 0; nIndex < m_szaCollection.GetCount(); nIndex++ )
	{
		szTemp = m_szaCollection.GetAt( nIndex );
		if( !szTemp.IsEmpty() )
		{
			nSel = m_ctrlCollection.AddString( szTemp );
			if( m_nCollectionID == appConfig.GetIDFromString( szTemp ))
				nCurSel = nSel;
		}
	}
	m_ctrlCollection.SetCurSel( nCurSel );

	// get library descriptions
	appConfig.m_SQL.GetDescriptions( CMySQLEx::library, &m_szaILLLibrary, CMySQLEx::list_ILL );
	// set m_ctrlILLLibrary
	m_ctrlILLLibrary.Init();
	m_ctrlILLLibrary.SetMode();
	for( INT nIndex = 0; nIndex < m_szaILLLibrary.GetCount(); nIndex++ )
	{
		szTemp = m_szaILLLibrary.GetAt( nIndex );
		if( !szTemp.IsEmpty() )
		{
			nSel = m_ctrlILLLibrary.AddString( szTemp );
			if( m_nILLLibraryID == appConfig.GetIDFromString( szTemp ))
				nCurSel = nSel;
		}
	}
	m_ctrlILLLibrary.SetCurSel( nCurSel );

	// disable default collection
	if( m_nCollectionID < 0 )
	{
		GetDlgItem( IDC_COMBO_COLLECTION )->EnableWindow( FALSE );
		m_bEnableDefaultCollection = FALSE;
	}
	else
	{
		m_bEnableDefaultCollection = TRUE;
	}

	// disable default ILL Library
	if( m_nILLLibraryID < 0 )
	{
		GetDlgItem( IDC_COMBO_ILL_LIBRARY )->EnableWindow( FALSE );
		m_bEnableDefaultILLLibrary = FALSE;
	}
	else
	{
		m_bEnableDefaultILLLibrary = TRUE;
	}

	// disable my view
	if( appConfig.m_Options.bDisableMyView )
	{
		m_bShowMyView = FALSE;
		GetDlgItem( IDC_CHECK_SHOW_MY_VIEW )->ShowWindow( SW_HIDE );
	}
	// hide print template if not specified
	if( appConfig.m_Printing.szRequestTemplate.IsEmpty() )
		GetDlgItem( IDC_CHECK_USE_PRINT_TEMPLATE )->ShowWindow( SW_HIDE );

	// remove password for LDAP
	if( appConfig.m_LDAP.bUseLDAPLogons )
	{
		GetDlgItem( IDC_STATIC_PASSWORD )->ShowWindow( SW_HIDE );
		GetDlgItem( IDC_EDIT_PASSWORD )->ShowWindow( SW_HIDE );
		GetDlgItem( IDC_BUTTON_PASSWORD )->ShowWindow( SW_HIDE );
	}

	// disable ILL
	if( appConfig.m_Options.bDisableILL )
	{
		GetDlgItem( IDC_STATIC_ILL )->ShowWindow( SW_HIDE );
		GetDlgItem( IDC_COMBO_ILL_LIBRARY )->ShowWindow( SW_HIDE );
		GetDlgItem( IDC_CHECK_ENABLE_DEFAULT_ILL_LIBRARY )->ShowWindow( SW_HIDE );
	}

	// get local settings
	m_bPromptMaterial = (BOOL)AfxGetApp()->GetProfileInt( _T("Settings"), _T("PromptMaterial"), 1 );

	// update data
	UpdateData( FALSE );

	return TRUE;  // return TRUE unless you set the focus to a control
	// EXCEPTION: OCX Property Pages should return FALSE
}

void CDlgEditStaff::OnOK()
{
	// update data
	UpdateData();

	if( m_szUserName.IsEmpty() )
	{
		AfxMessageBox( _T("Please enter at least a user name.") );
		return;
	}

	// check if adding
	if( m_bAddStaff )
	{
		// check if exists
		LONG nCount = appConfig.m_SQL.GetRowCount( CMySQLEx::staff, _T("name"), m_szUserName );
		if( nCount > 0 )
		{
			AfxMessageBox( _T("Name address already exists, please use a different email address.") );
			return;
		}
	}

	// check if default collection is enabled
	if( m_bEnableDefaultCollection )
	{
		// get collection id
		CString szTemp;
		m_ctrlCollection.GetWindowText( szTemp );
		// check collection
		m_nCollectionID = appConfig.GetIDFromString( szTemp );
		if( m_nCollectionID < 0 )
			return;
	}
	else
	{
		// set deafult to -1 to disable
		m_nCollectionID = -1;
	}

	// check if default ill library is enabled
	if( m_bEnableDefaultILLLibrary )
	{
		// get collection id
		CString szTemp;
		m_ctrlILLLibrary.GetWindowText( szTemp );
		// set user filter				
		m_nILLLibraryID = appConfig.GetIDFromString( szTemp );
		if( m_nILLLibraryID < 0 )
			return;
	}
	else
	{
		// set deafult to -1 to disable
		m_nILLLibraryID = -1;
	}

	// set current
	STAFF_SET Staff;

	// change password
	if( m_bChangePwd || m_bAddStaff )
	{
		// set password
		CString szHash;
		// convert for hash
		szHash = m_szUserPassword;
		appConfig.Hash( &szHash );
		// change password
		Staff.m_pwd = szHash;
	}
	else
	{
		// same password
		Staff.m_pwd = m_szUserPassword;
	}

	//StaffSet.m_date_login.Format( appConfig.m_General.szDateFormat ) = m_szDateLogin;
	Staff.m_name = m_szUserName;
	Staff.m_email = m_szUserEmail;
	Staff.m_phone = m_szUserPhone;
	Staff.m_level = _tstol( m_szUserLevel );
	Staff.m_library_id = _tstol( m_szLibraryID );
	Staff.m_show_triage = m_bShowTriage;
	Staff.m_show_library_view = m_bShowLibraryView;
	Staff.m_show_my_view = m_bShowMyView;
	Staff.m_show_process_view = m_bShowInProcessView;
	Staff.m_show_current_view = m_bShowCurrentView;
	Staff.m_show_completed_view = m_bShowCompletedView;
	Staff.m_show_hold_view = m_bShowHoldView;
	Staff.m_show_highlights = m_bShowHighlights;
	Staff.m_show_duplicates = m_bHighlightDuplicate;
	Staff.m_sort_ascending = m_bSortAscending;
	Staff.m_print_full_request = TRUE;
	Staff.m_bold_email_print = m_bBoldEmailOnPrint;
	Staff.m_enable_thumbnail = m_bEnableThumbnail;
	Staff.m_completed_days = 0;
	Staff.m_use_print_template = m_bUsePrintTemplate;
	Staff.m_print_per_page = m_nPrintPerPage;
	Staff.m_default_collection = m_nCollectionID;
	Staff.m_default_ill_library = m_nILLLibraryID;

	// save it
	appConfig.m_SQL.SetStaffSet( &Staff, m_nStaffID, m_bAddStaff );

	// save local settings
	AfxGetApp()->WriteProfileInt( _T("Settings"), _T("PromptMaterial"), m_bPromptMaterial );

	// close dialog
	CDialog::OnOK();
}

void CDlgEditStaff::OnBnClickedButtonPassword()
{
	CDlgChangePwd dlgPassword;
	// show dialog
	if( dlgPassword.DoModal() == IDOK )
	{
		m_bChangePwd = TRUE;
		m_szUserPassword = dlgPassword.m_szPassword;
		//update data
		UpdateData( FALSE );
	}
}

void CDlgEditStaff::OnBnClickedButtonDelete()
{
	// check to make sure
	CString szMessage;
	szMessage.Format( _T("Deleting staff member will also release any pending requests.\n\nAre you sure you want to delete %s (ID=%ld)?"), m_szUserName, m_nStaffID );
	if( AfxMessageBox( szMessage ) == IDNO )
		return;

	CDlgConfirm dlgConfirm;
	dlgConfirm.m_szAction = _T("This will permantly delete this staff member.\n\nAre you sure you want to proceed?");
	if( dlgConfirm.DoModal() != IDOK )
		return;

	// remove row
	appConfig.m_SQL.RemoveRow( CMySQLEx::staff, m_nStaffID );

	// adjust requests
	appConfig.m_SQL.UpdateFieldValue( CMySQLEx::requests, _T("staff_id"), m_nStaffID, 0 );

	// set new user Id
	m_nStaffID = -1;

	CString szName;
	CDlgPickUser dlgPickUser;
	dlgPickUser.m_nType = dlgPickUser.PICK_TYPE_STAFF;
	if( dlgPickUser.DoModal() == IDOK )
		szName = dlgPickUser.m_szName;
	else
	{
		EndDialog( IDCANCEL );
		return;
	}

	STAFF_SET Staff;
	appConfig.m_SQL.GetStaffSet( &Staff, szName );

	// set user id
	m_nStaffID = Staff.m_id;
	// set current
	m_szID.Format( _T("%.8ld"), Staff.m_id );
	m_szDateLogin = Staff.m_date_login.Format( appConfig.m_General.szDateFormat );
	m_szUserName = Staff.m_name;
	m_szUserEmail = Staff.m_email;
	m_szUserPhone = Staff.m_phone;
	m_szUserLevel.Format( _T("%d"), Staff.m_level );
	m_szLibraryID.Format( _T("%d"), Staff.m_library_id );
	m_bShowTriage = (BOOL)Staff.m_show_triage;
	m_bShowLibraryView = (BOOL)Staff.m_show_library_view;
	m_bShowMyView = (BOOL)Staff.m_show_my_view;
	m_bShowInProcessView = (BOOL)Staff.m_show_process_view;
	m_bShowCurrentView = (BOOL)Staff.m_show_current_view;
	m_bShowCompletedView = (BOOL)Staff.m_show_completed_view;
	m_bShowHoldView = (BOOL)Staff.m_show_hold_view;
	m_bShowHighlights = (BOOL)Staff.m_show_highlights;
	m_bHighlightDuplicate = (BOOL)Staff.m_show_duplicates;
	m_bSortAscending = (BOOL)Staff.m_sort_ascending;
	m_bBoldEmailOnPrint = (BOOL)Staff.m_bold_email_print;
	m_bEnableThumbnail = (BOOL)Staff.m_enable_thumbnail;
	m_bUsePrintTemplate = (BOOL)Staff.m_use_print_template;
	m_nPrintPerPage = Staff.m_print_per_page;

	// update data
	UpdateData( FALSE );
}

void CDlgEditStaff::OnBnClickedCheckEnableDefaultCollection()
{
	// update data
	UpdateData();

	// check for checked
	if( m_bEnableDefaultCollection )
	{
		GetDlgItem( IDC_COMBO_COLLECTION )->EnableWindow();
	}
	else
	{
		GetDlgItem( IDC_COMBO_COLLECTION )->EnableWindow( FALSE );
		m_nCollectionID = 0;
	}
}

void CDlgEditStaff::OnBnClickedCheckEnableDefaultIllLibrary()
{
	// update data
	UpdateData();

	// check for checked
	if( m_bEnableDefaultILLLibrary )
	{
		GetDlgItem( IDC_COMBO_ILL_LIBRARY )->EnableWindow();
	}
	else
	{
		GetDlgItem( IDC_COMBO_ILL_LIBRARY )->EnableWindow( FALSE );
		m_nILLLibraryID = 0;
	}	
}
